package defaultPackage;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URLClassLoader;

import javax.imageio.ImageIO;

import pathfind.*;
import taskMgr.Task;
import taskMgr.TaskBuildTile;
import taskMgr.TaskStatus;

public class Person 
{
        int count = 0;
        int x,y,xDestination,yDestination,stepCount = 0;
        String name = "defalt";
        public NodeMap map;
        CrewType type;
        public Path path;
        private PathFinder pathfinder;
        private boolean debug = false, hasDestination = false;
        private int id;
        private static int nextid = 0;
        public MainClass main;
        boolean selected = false,userSetDestination = false;
        int health;
        double healthPercent;
        boolean wearingSpaceSuit = false;
        int personalOxygen = 0;
        BufferedImage spaceSuitSprite = null;
        Task currentTask;
        boolean hasTask = false;
        
        public Person(MainClass main,int x,int y,String name, CrewType type)
        {
                health = type.startHealth;
                this.main=main;
                this.type = type;
                id = nextid;
                nextid++;
                this.name=name;
                this.x=x;
                this.y=y;
                map = new NodeMap(Grid.dimensionx, Grid.dimensiony);
                pathfinder = new PathFinder(map);
                try {
                        spaceSuitSprite = ImageIO.read(URLClassLoader.getSystemResource("res/Sprites/"+"SpaceSuit.png"));
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
        public Person(int x,int y)
        {
                id = nextid;
                nextid++;
                this.x=x;
                this.y=y;
                map = new NodeMap(Grid.dimensionx, Grid.dimensiony);
                setmap();
                main.grid.tiles[x][y].hasPerson = true;
        }
        public int getid()
        {
                return id;
        }
        public void draw(Graphics g)
        {
                if(debug && hasDestination)
                {
                        for(int x = 0;x<map.getsizeX();x++)
                        {
                                for(int y = 0;y<map.getsizeY();y++)
                                {
                                        g.setColor(Color.red);
                                        g.drawString(""+map.getNode(x, y)%10, x*10, y*10+10);
                                }
                        }
                        g.setColor(Color.green);
                        for(Point p:path.getList())
                                g.fillRect(p.getx()*10, p.gety()*10, 10, 10);
                }
                if (wearingSpaceSuit)
                {
                        g.drawImage(spaceSuitSprite, x*10, y*10, null);
                }
                else
                        type.draw(g, x, y);
                
                if(selected)
                {
                        g.setColor(new Color(0,0,255,90));
                        g.fillRect(x*10, y*10, 10, 10);
                }
        }
        public void setDestination(int x,int y)
        {
                hasDestination = true;  
                this.xDestination = x;
                this.yDestination = y;
                setmap();
                pathfinder.start(x, y);
                pathfinder.run();
                path = new Path(map,this.x,this.y);

        }
        public void update()
        {       
                if(!hasTask)
                        checkForTask();
                if (hasTask && currentTask.getStatus() == TaskStatus.inProgress)
                        currentTask.action(this);
                if (hasTask && !currentTask.canReachTask(this))
                {
                        finishCurrentTask();
                }
                        
                
                
                if(x == xDestination && y == yDestination)
                        userSetDestination = false;
                if(hasDestination)
                {
                        if (stepCount < type.speed)
                        {
                                stepCount++;
                        }
                        else
                        {
                                if(path.getList().size()<=1)
                                        hasDestination=false;
                                else
                                {
                                        step();
                                        stepCount = 0;
                                }
                        }
                }
                if (personalOxygen <= 0)
                {
                        for (int jx = -2;jx<=2;jx++)
                        {
                                for (int jy = -2;jy<=2;jy++)
                                {
                                        if (main.grid.tiles[x+jx][y+jy].oxygen > main.grid.tiles[x][y].oxygen+5 && !main.grid.tiles[jx+x][jy+y].type.getSolid() && !userSetDestination && main.grid.tiles[x+jx][y+jy].oxygen > main.grid.tiles[xDestination][yDestination].oxygen && !main.grid.tiles[x+jx][y+jy].hasPerson)
                                        {
                                                setDestination(x+jx, y+jy);
                                        }
                                }
                        }
                }
                if (main.grid.tiles[x][y].oxygen<10 && health > 0 && !wearingSpaceSuit)
                        if (count >= ((int)((double)main.grid.tiles[x][y].oxygen/5+.5)))
                        {
                                health--;
                                count = 0;
                        }
                        else if(count != 3)
                                count++;
                healthPercent = health*100/type.startHealth;
                
                if (main.grid.tiles[x][y].oxygen<10 && health > 0 && wearingSpaceSuit)
                {
                        if (count >= 25 && personalOxygen > 0)
                        {
                                personalOxygen--;
                                count = 0;
                        }
                        else if (count >=3 && health > 0 && personalOxygen <= 0)
                        {
                                health--;
                                count = 0;
                        }
                        else
                                count++;
                }
        }
        private void step()
        {
                main.grid.tiles[x][y].hasPerson = false;
                if(path.getList().size() != 0)
                {
                        x=path.get(1).getx();
                        y=path.get(1).gety();
                }else hasDestination=false;
                if(x==xDestination && y==yDestination || !hasDestination)
                        hasDestination=false;
                else
                {
                        setmap();
                        pathfinder.start(xDestination, yDestination);
                        pathfinder.run();
                        path = new Path(map, x, y);
                }
                main.grid.tiles[x][y].hasPerson = true;
        }
        public void setmap()
        {
                for(int y = 0;y<Grid.dimensiony;y++)
                {
                        for(int x = 0;x<Grid.dimensionx;x++)
                        {
                                int block = 0;
                                if(main.grid.tiles[x][y].type.getSolid())       block = -1;
                                
                                map.setblock(x, y, block);
                        }
                }
        }
        public void setDebug(boolean debug)
        {
                this.debug=debug;
        }
        public boolean isDebug()
        {
                return debug;
        }
        
        public boolean isSelected()
        {
                return selected;
        }
        
        public void setSelected(boolean selected)
        {
                if (selected == true)
                        main.people.someonesSelected = true;
                else 
                        main.people.someonesSelected = false;
                this.selected = selected;
        }
        
        public int getHealth()
        {
                return health;
        }
        
        public void setHealth(int health)
        {
                this.health = health;
        }
        
        public void setWearingSpaceSuit(boolean a)
        {
                this.wearingSpaceSuit = a;
                personalOxygen = 100;
        }
        
        public boolean isWearingSpaceSuit()
        {
                return wearingSpaceSuit;
        }
        
        public void checkForTask()
        {
                for(Task t:main.taskManager.taskList)
                {
                        if(t.getNumPeople() < 3)
                        {
                                if (!hasTask)
                                {
                                        currentTask = t;
                                        t.setStatus(TaskStatus.inProgress);
                                        hasTask = true;
                                        t.setNumPeople(t.getNumPeople()+1);
                                        main.grid.tiles[t.x][t.y].setJobHere(true);
                                }
                        }
                }
        }
        
        public int getX()
        {
                return x;
        }
        
        public int getY()
        {
                return y;
        }
        
        public int getDesX()
        {
                return xDestination;
        }
        
        public int getDesY()
        {
                return yDestination;
        }
        
        public boolean hasDestination()
        {
                return hasDestination;
        }
        
        public void setHasDestination(boolean hasDestination)
        {
                this.hasDestination = hasDestination;
        }
        
        public void finishCurrentTask()
        {
                main.grid.tiles[currentTask.x][currentTask.y].setJobHere(false);
                main.taskManager.removeTask(currentTask);
                hasTask = false;
                hasDestination = false;
        }
        public Task getCurrentTask()
        {
                return currentTask;
        }
        
}